<?php
defined("_VALID_ACCESS") || die('Direct access forbidden');
class Eyeworx_PatientRecords_Eyes extends Module
{
    public function construct()
    {
        Base_RegionalSettingsCommon::set_tz();
        load_css('modules/Utils/GenericBrowser/theme/default.css');
        load_css('modules/Utils/RecordBrowser/theme/View_entry.css');
    }

    public function body()
    {
        $rboeye_recordset = new Eyeworx_PatientRecords_Eyes_RBO_RBOEyes();
        $this->display_module($rboeye_recordset->create_rb_module($this));
    }

    public function patient_rx_addon($r, $rb)
    {
        $form = & $this->init_module('Libs/QuickForm');
        $theme = & $this->init_module('Base/Theme');

        $form->addElement('button', 'add_rx_button', __('Add'), array('onclick' => "jQuery('#add_new_rx').val('1');" . $form->get_submit_form_js(), 'id' => 'add_rx_button', 'class' => 'button'));
//        $form->addElement('button', 'add_rx_button', __('Add'), $form->get_submit_form_href());
        $form->addElement('hidden', 'add_new_rx', '', array('id' => 'add_new_rx'));

        $form->addElement('datepicker', 'date', __('Date'), array('style' => 'width:97%'));

        $form->addElement('text', 'od_sphere', '', array('style' => 'width:97%;', 'id' => 'od_sphere'));
        $form->addElement('text', 'od_cylinder', '', array('style' => 'width:97%;', 'id' => 'od_cylinder'));
        $form->addElement('text', 'od_axis', '', array('style' => 'width:97%;', 'id' => 'od_axis'));
        $form->addElement('text', 'od_add', '', array('style' => 'width:97%;', 'id' => 'od_add'));

        $form->addElement('text', 'os_sphere', '', array('style' => 'width:97%;', 'id' => 'os_sphere'));
        $form->addElement('text', 'os_cylinder', '', array('style' => 'width:97%;', 'id' => 'os_cylinder'));
        $form->addElement('text', 'os_axis', '', array('style' => 'width:97%;', 'id' => 'os_axis'));
        $form->addElement('text', 'os_add', '', array('style' => 'width:97%;', 'id' => 'os_add'));

        $form->addElement('text', 'pd', 'P.D.', array('style' => 'width:97%;', 'id' => 'pd'));
        $form->addElement('text', 'lens_type', 'Lens Type', array('style' => 'width:97%;', 'id' => 'lens_type'));
        $form->addElement('text', 'index', 'Index', array('style' => 'width:97%;', 'id' => 'index'));
        $form->addElement('text', 'others', 'Others', array('style' => 'width:97%;', 'id' => 'others'));

        $form->addElement('text', 'odmd', 'O.D./M.D.', array('style' => 'width:97%;', 'id' => 'odmd'));
        $form->addElement('text', 'license_no', 'License No', array('style' => 'width:97%;', 'id' => 'license_no'));
        $form->addElement('button', 'save_rx', __('Save'), array('onclick' => "jQuery('#add_new_rx').val('2');" . $form->get_submit_form_js(), 'id' => 'save_rx', 'class' => 'button'));
        $form->addElement('button', 'cancel_rx', __('Cancel'), array('onclick' => "jQuery('#add_new_rx').val('3');" . $form->get_submit_form_js(), 'class' => 'button'));

        $form->setDefaults(array('date' => date("m/d/Y", time())));

        $add_mode = array();
        $save = false;
        $eye_record = null;
        if ($form->isSubmitted() && $form->validate()) {
            $values = $form->getSubmitValues();
            foreach ($values as $key => $value) {
                if ($key == 'od_sphere' && $value != '') $eye_record['od1'] = $value;
                if ($key == 'od_cylinder' && $value != '') $eye_record['od2'] = $value;
                if ($key == 'od_axis' && $value != '') $eye_record['od3'] = $value;
                if ($key == 'od_add' && $value != '') $eye_record['od4'] = $value;
                if ($key == 'os_sphere' && $value != '') $eye_record['os1'] = $value;
                if ($key == 'os_cylinder' && $value != '') $eye_record['os2'] = $value;
                if ($key == 'os_axis' && $value != '') $eye_record['os3'] = $value;
                if ($key == 'os_add' && $value != '') $eye_record['os4'] = $value;
                if ($key == 'pd' && $value != '') $eye_record['o1'] = $value;
                if ($key == 'lens_type' && $value != '') $eye_record['o2'] = $value;
                if ($key == 'index' && $value != '') $eye_record['o3'] = $value;
                if ($key == 'others' && $value != '') $eye_record['o4'] = $value;
                if ($key == 'date' && $value != '') $eye_record['o5'] = $value;
                if ($key == 'odmd' && $value != '') $eye_record['o6'] = $value;
                if ($key == 'license_no' && $value != '') $eye_record['o7'] = $value;

                if ($key == 'add_new_rx' && $value != '') {
                    if($value == '1') $add_mode[] = '0';
                    else if($value == '2') $save = true;
                    else if($value == '3') location();
                }
            }
            var_dump($r['id'], $save, $eye_record);
            if($save && !is_null($eye_record)) {
                $eyes_recordset = new RBO_RecordsetAccessor('eyeworx_eyes');
                $rc = $eyes_recordset->new_record();
                $rc->patient = $r['id'];
                $rc->date = date("Y-m-d", strtotime($eye_record['o5']));
                $rc->od_sphere = $eye_record['od1'];
                $rc->od_cylinder = $eye_record['od2'];
                $rc->od_axis = $eye_record['od3'];
                $rc->od_add = $eye_record['od4'];
                $rc->os_sphere = $eye_record['os1'];
                $rc->os_cylinder = $eye_record['os2'];
                $rc->os_axis = $eye_record['os3'];
                $rc->os_add = $eye_record['os4'];
                $rc->pd = $eye_record['o1'];
                $rc->lens_type = $eye_record['o2'];
                $rc->index_eyes = $eye_record['o3'];
                $rc->others = $eye_record['o4'];
                $rc->od_or_md = $eye_record['o6'];
                $rc->license_number = $eye_record['o7'];
                $rc->save();
//                location();
            }
        } else {
            $rs = new Eyeworx_PatientRecords_Eyes_RBO_RBOEyes();
            $this->rb = $rs->create_rb_module($this);
            $crits = array("patient" => $r['id']);
            $cols = array("patient" => false, "date" => true, "od_sphere" => true, "od_cylinder" => true, "od_axis" => true, "od_add" => true, "os_sphere" => true, "os_cylinder" => true, "os_axis" => true, "os_add" => true, "pd" => true, "index_eyes" => true, "others" => true);
            $order = array("date" => "desc");
            $show_data_params = array($crits, $cols, $order);
            $this->rb = $this->init_module('Utils/RecordBrowser', 'eyeworx_eyes');
            $this->rb->set_defaults(array('employee' => $r['id']));
            $this->rb->set_button('');
            $this->display_module($this->rb, $show_data_params, 'show_data');
        }
        $form->assign_theme('submit_form', $theme);
        $theme->assign('add_mode', $add_mode);
        $theme->display();
    }

    public function caption()
    {
        return "Patient Rx Information";
    }
}

?>